﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RuoVea.ExFilter.Config;
using RuoVea.ExFilter.Model;

namespace RuoVea.ExFilter;

/// <summary>
/// IOptions 扩展
/// </summary>
public static class IOptions
{
    /// <summary>
    /// 注入 全局异常处理 [进行全局的异常日志收集]
    /// </summary>
    /// <param name="services"></param>
    public static void Exception(this IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            //全局注册
            options.Filters.Add<ExceptionFilter>();
        });
        // 注册EventBus服务
        services.AddEventBus(builder => { });
    }

    /// <summary>
    /// 注入 请求日志拦截 [执行操作日志、参数验证，权限控制 ]
    /// </summary>
    public static void Action(this IServiceCollection services, Action<ExFilterOptions>? action = null)
    {
        services.AddMvcCore(options =>
        {
            //执行操作日志、参数验证
            options.Filters.Add(typeof(ActionFilter));
        });
        // 注册EventBus服务
        services.AddEventBus(builder =>{});
        if (action != null)
        {
            if (ExFilterConfigs.FilterOptions == null)
                ExFilterConfigs.FilterOptions = new ExFilterOptions();
            action.Invoke(ExFilterConfigs.FilterOptions);
        }
    }

    /// <summary>
    /// 接口appkey和时间戳校验
    /// </summary>
    /// <param name="services"></param>
    public static void SafeAction(this IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            //接口appkey和时间戳校验
            options.Filters.Add(typeof(SafeFilter));
        });
    }
    /// <summary>
    /// 注入 对资源型信息进行过滤。常用于防盗链/资源缓存
    /// </summary>
    public static void Resource(this IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            //防盗链
            options.Filters.Add(typeof(ResourceFilter));
        });
    }

    /// <summary>
    /// 结果格式化处理
    /// </summary>
    /// <param name="services"></param>
    public static void Result(this IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            //防盗链
            options.Filters.Add(typeof(ResultFilter));
        });
    }
}
